package com.hngy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.hngy.common.BaseContext;
import com.hngy.common.R;
import com.hngy.domain.AddressBook;
import com.hngy.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author LiKe
 * @date 2021/12/31 9:53
 */
@RestController
@RequestMapping("/addressBook")
@Slf4j
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    /**
     * 查询所有地址
     * @param session
     * @return
     */
    @GetMapping("/list")
    public R list(HttpSession session){

        String userId = session.getAttribute("user").toString();

        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(AddressBook::getUserId,userId);

        List<AddressBook> list = addressBookService.list(queryWrapper);

        return R.success(list);
    }

    /**
     * 回显
     * @param addressBookId
     * @return
     */
    @GetMapping("/{addressBookId}")
    public R getById(@PathVariable Long addressBookId){
        AddressBook addressBook = addressBookService.getById(addressBookId);
        return R.success(addressBook);
    }
    @PutMapping
    public R update(@RequestBody AddressBook addressBook){

        addressBookService.updateById(addressBook);

        return R.success("修改成功！");
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public R delById(@RequestParam List<Long> ids){

        addressBookService.removeByIds(ids);
        return R.success("删除成功！");
    }

    /**
     * 新增
     * @param session
     * @param addressBook
     * @return
     */
    @PostMapping
    public R add(HttpSession session,@RequestBody AddressBook addressBook){
        addressBook.setUserId((Long) session.getAttribute("user"));
        addressBookService.save(addressBook);
        return R.success("添加成功！");
    }

    @PutMapping("/default")
    public R updateDefault(HttpSession session,@RequestBody AddressBook addressBook){

        LambdaUpdateWrapper<AddressBook> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(AddressBook::getIsDefault,0);
        updateWrapper.eq(AddressBook::getUserId,(Long) session.getAttribute("user"));
        addressBookService.update(updateWrapper);

        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);

        return R.success("修改成功！");
    }
    /**
     * 查询默认地址
     */
    @GetMapping("default")
    public R getDefault() {
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId, BaseContext.getId());
        queryWrapper.eq(AddressBook::getIsDefault, 1);

        //SQL:select * from address_book where user_id = ? and is_default = 1
        AddressBook addressBook = addressBookService.getOne(queryWrapper);

        if (null == addressBook) {
            return R.error("没有找到该对象");
        } else {
            return R.success(addressBook);
        }
    }
}
